
import instance from "./axios";

const axios = ({
    method,
    url,
    data,
    config
}) => {
    method = method.toLowerCase();
    if (method == 'post') {
        return instance.post(url, data, { ...config })
    } else if (method == 'get') {
        return instance.get(url, {
            params: data,
            ...config
        })
    } else if (method == 'delete') {
        return instance.delete(url, {
            params: data,
            ...config
        },)
    } else if (method == 'put') {
        return instance.put(url, data, { ...config })
    } else {
        console.error('未知的method' + method)
        return false
    }
}

export default {
    install: (app) => {
        app.config.globalProperties.$get = (url, data, config) => {
            return axios({
                url: url,
                method: "get",
                data,
                config
            })
        };
        app.config.globalProperties.$post = (url, data, config) => {
            return axios({
                url: url,
                method: "post",
                data,
                config
            })
        };
        app.config.globalProperties.$put = (url, data, config) => {
            return axios({
                url: url,
                method: "put",
                data,
                config
            })
        };
        app.config.globalProperties.$delete = (url, data, config) => {
            return axios({
                url: url,
                method: "delete",
                data,
                config
            })
        };

        app.config.globalProperties.$postForm = (url, data) => {
            return axios({
                url: url,
                method: "post",
                data,
                config: {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }
            })
        };
        app.config.globalProperties.$putForm = (url, data) => {
            return axios({
                url: url,
                method: "put",
                data,
                config: {
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                    }
                }
            })
        };
        app.config.globalProperties.$upload = (url, data) => {
            return axios({
                url: url,
                method: "post",
                data,
                config: {
                    timeout: 60 * 1000,
                    headers: {
                        'Content-Type': 'multipart/form-data'
                    }
                }
            })
        };
        app.config.globalProperties.$download = (url, data) => {
            return axios({
                method: "post",
                url: url,
                data: data,
                config: {
                    timeout: 60 * 1000 * 10,
                    responseType: "blob"
                }
            }).then(res => {
                let blob = new Blob([res.data], {
                    type: "application/vnd.ms-excel;charset=utf-8"
                });
                let contentDisposition = res.headers["content-disposition"];
                let patt = new RegExp("filename=([^;]+\\.[^\\.;]+);*");
                let result = patt.exec(contentDisposition);
                let filename = decodeURIComponent(result[1]);
                let downloadElement = document.createElement("a");
                let href = window.URL.createObjectURL(blob);
                downloadElement.style.display = "none";
                downloadElement.href = href;
                downloadElement.download = filename;
                document.body.appendChild(downloadElement);
                downloadElement.click();
                document.body.removeChild(downloadElement);
                window.URL.revokeObjectURL(href);
            });
        };
    }
};
